//
//  MYGAddFriendViewController.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/4/25.
//

import UIKit
import MJRefresh
class MYGAddFriendViewController: MYGBaseController {
    var contents: [MYGFriendModel] = []
    private var page = 1
    override func viewDidLoad() {
        super.viewDidLoad()

        configUI()
        getList()
    }
    private func configUI() {
        title = "消息"

        view.addSubview(tableView)
        view.addSubview(headerView)
        headerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(15)
            make.top.equalToSuperview()
            make.height.equalTo(170)
        }
     
        view.addSubview(lineView)
        view.addSubview(nameLabel)
        
        lineView.snp.makeConstraints { make in
            make.top.equalTo(headerView.snp.bottom).offset(20)
            make.left.equalToSuperview().inset(17)
            make.width.equalTo(3)
            make.height.equalTo(15)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(lineView.snp.right).offset(10)
            make.centerY.equalTo(lineView)
        }
        
        tableView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(15)
            make.bottom.equalToSuperview()
            make.top.equalTo(lineView.snp.bottom).offset(15)
        }
        
    }
    
    private lazy var headerView:MYGAddFriendHeaderView = {
        let v = MYGAddFriendHeaderView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 170))
        
        return v
    }()
    
    private lazy var lineView: UIView = {
        let view = UIView()
        let gl = CAGradientLayer()
        gl.colors = [UIColor.rgba(202, 172, 255, 1).cgColor, UIColor.rgba(143, 134, 255, 1).cgColor]
        gl.locations = [0, 1]
        gl.frame = CGRect(x: 0, y: 0, width: 3, height: 15)
        gl.startPoint = CGPoint(x: 0, y: 0)
        gl.endPoint = CGPoint(x: 1, y: 1)
        
        gl.masksToBounds = true
        gl.cornerRadius = 1.5
        view.layer.addSublayer(gl)
        return view
    }()
    
    private(set) lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(18)
        label.text = "你可能认识的人"
        label.textColor = .rgba(34, 34, 34, 1)
        return label
    }()
    private lazy var tableView: UITableView = {
        let tableV = UITableView()
        tableV.separatorStyle = .none
        tableV.delegate = self
        tableV.dataSource = self
        tableV.backgroundColor = .rgba(250, 250, 250, 1)
        tableV.register(cellWithClass: MYGAddFriendTableViewCell.self)
        tableV.mj_header = MJRefreshNormalHeader(refreshingBlock: {[weak self] in
            self?.page = 1
            self?.getList()
        })
        
        tableV.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: {[weak self] in
            self?.page += 1
            self?.getList()
        })
        return tableV
    }()
 
}
extension MYGAddFriendViewController {
    
    func getList(){
        MYGLoading.show()
        MMYGNetwork<MYGMessageApi, MYGPageModel<MYGFriendModel>>.request(target:.mayKnow(page: self.page)) { [weak self] item in
            MYGLoading.hide()
            guard let item = item else {
                return
            }
            self!.tableView.mj_header?.endRefreshing()
            if item.page.page <= 1 {
                self!.contents = item.list
            } else {
                self!.contents.append(contentsOf: item.list)
            }
            
            if item.page.total <= self!.contents.count {
                self!.tableView.mj_footer?.endRefreshingWithNoMoreData()
            } else {
                self!.tableView.mj_footer?.endRefreshing()
            }
            self!.tableView.reloadData()
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
    }
    
    
    
}
extension MYGAddFriendViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        contents.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        70
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: MYGAddFriendTableViewCell.self, for: indexPath)
        cell.model = contents[indexPath.row]
        cell.relaodData = { [self] in
            getList()
        }
        return cell
    }
}
